﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZebraCoreLib.Framework;
using System.Data.Linq.Mapping;
using ZebraCoreLib.ZebraException;
using ZebraCoreLib.BasBiz;
using System.Data.Common;
using ZebraCoreLib.Logistics;


namespace ZebraCoreLib.Stock
{
    [ZebraBizObject(TableName = "osPortGoods")]
    public class GoodsStock : BasBizService<GoodsStock>
    {

        [ZebraField(Title = "编号", DataField = "sgID", IsRequired = true, IsPrimaryKey = true, IsAutoID = true)]
        public string SgID { get; set; }
        [ZebraField(Title = "转运单号", DataField = "sgCode", MinSize = 8, MaxSize = 32, IsRequired = true, ToolTip = "转运单号")]
        public string SgCode { get; set; }

        [ZebraField(Title = "仓位", DataField = "sgStoreCode", IsRequired = true)]
        public string SgStoreCode { get; set; }

        [ZebraField(Title = "入库时间", DataField = "sgDate", IsRequired = true)]
        public string SgDate { get; set; }

        [ZebraField(Title = "入库时间", DataField = "sgTime", IsRequired = true, SortOrder = true, SortByDesc = true)]
        public string SgTime { get; set; }

        [ZebraField(Title = "仓库", DataField = "sgArea", IsRequired = true, WaterMark="请输入仓库",ToolTip="有哪些仓库可以输入……")]
        public string SgArea { get; set; }

        [ZebraField(Title = "客户标识码", DataField = "sgPin", IsRequired = true)]
        public string SgPin { get; set; }

        [ZebraField(Title = "", DataField = "sgShipper")]
        public string SgShipper { get; set; }

        [ZebraField(Title = "入库备注", DataField = "sgRemark", MinSize = 0, MaxSize = 200, IsRequired = true)]
        public string SgRemark { get; set; }

        [ZebraField(Title = "", DataField = "sgFlag")]
        public string SgFlag { get; set; }

        [ZebraField(Title = "", DataField = "sgPort")]
        public string SgPort { get; set; }

        [ZebraField(Title = "入库重量", DataField = "sgLbs")]
        public double SgLbs { get; set; }

        [ZebraField(Title = "", DataField = "sgStatus")]
        public string SgStatus { get; set; }

        [ZebraField(Title = "", DataField = "sgExpress")]
        public string SgExpress { get; set; }

        [ZebraField(Title = "", DataField = "sgCargoStatus")]
        public string SgCargoStatus { get; set; }

        [ZebraField(Title = "", DataField = "sgSelfID")]
        public string SgSelfID { get; set; }

        [ZebraField(Title = "", DataField = "sgReceOrgID")]
        public string SgReceOrgID { get; set; }

        [ZebraField(Title = "", DataField = "sgCheckTime")]
        public string SgCheckTime { get; set; }

        [ZebraField(Title = "", DataField = "sgCheckStatus")]
        public string SgCheckStatus { get; set; }

        [ZebraField(Title = "", DataField = "outPut_Flag")]
        public string OutPut_Flag { get; set; }

        [ZebraField(Title = "长", DataField = "sgLength")]
        public double SgLength { get; set; }

        [ZebraField(Title = "宽", DataField = "sgWidth")]
        public double SgWidth { get; set; }

        [ZebraField(Title = "高", DataField = "sgHight")]
        public double SgHeight { get; set; }

        [ZebraField(Title = "体积重", DataField = "sgVolume")]
        public double SgVolume { get; set; }

        [ZebraField(Title = "长度单位", DataField = "sgLUnit")]
        public string SgLUnit { get; set; }

        [ZebraField(Title = "重量单位", DataField = "sgWunit")]
        public string SgWunit { get; set; }

        [ZebraField(Title = "", DataField = "sgCustomerRemark")]
        public string SgCustomerRemark { get; set; }

        [ZebraField(Title = "", DataField = "sgGoodsName")]
        public string SgGoodsName { get; set; }

        [ZebraField(Title = "箱号", DataField = "boxNO")]
        public string BoxNO { get; set; }

        [ZebraField(Title = "商品名称", DataField = "goodsName")]
        public string GoodsName { get; set; }

        [ZebraField(Title = "入库数量", DataField = "inBoundQty")]
        public int InBoundQty { get; set; }

        [ZebraField(Title = "可用数量", DataField = "remainQty")]
        public int RemainQty { get; set; }

        [ZebraField(Title = "出库数量", DataField = "outBoundQty")]
        public int OutBoundQty { get; set; }

        [ZebraField(Title = "", DataField = "parentID")]
        public string ParentID { get; set; }

        [ZebraField(Title = "入库信息(SKU)", Foreignkey = "parentID")]
        public List<GoodsStock> StockDetails { get; set; }

        [ZebraField(Title = "", DataField = "isUnpacked")]
        public string IsUnpacked { get; set; }

        [ZebraField(Title = "", DataField = "isHasTransPackage")]
        public string IsHasTransPackage { get; set; }

        [ZebraField(Title = "仓库信息", Relationship = "sacode=sgArea")]
        public LogisticsCenter LogisticsCenterArea { get; set; }



        public List<GoodsStock> List(string sucode, string area)
        {
            List<GoodsStock> list = new List<GoodsStock>();
            List<GoodsStock> goodStockList = List("suCode='" + sucode + "' and sgarea='" + area + "'");
            foreach (GoodsStock forGoodsStock in goodStockList)
            {
                List<GoodsStock> selectDoublelist = list.Where(p => p.ParentID == forGoodsStock.SgID) as List<GoodsStock>;
                if (selectDoublelist.Count() > 0)
                {
                    selectDoublelist[0].InBoundQty += forGoodsStock.InBoundQty;
                    selectDoublelist[0].OutBoundQty += forGoodsStock.OutBoundQty;
                    selectDoublelist[0].RemainQty += forGoodsStock.RemainQty;
                }
                else if (forGoodsStock.IsUnpacked != "1")
                {
                    list.Add(forGoodsStock);
                }
            }

            return list;
        }

        //public override List<GoodsStock> List(string parameters, ListFlags flags, int pageIndex, int pageSize, out int pageCount)
        //{
        //    pageCount = 0;
        //    List<GoodsStock> list = new List<GoodsStock>();
        //    int pagenum = 0;
        //    List<GoodsStock> goodStockList = base.List("sgpin='phcad' and sgarea='sha'", ListFlags.Details, 0, 0, out pagenum);
        //    foreach (GoodsStock forGoodsStock in goodStockList)
        //    {
        //        List<GoodsStock> selectDoublelist = list.Where(p => p.ParentID == forGoodsStock.SgID) as List<GoodsStock>;
        //        if (selectDoublelist!=null && selectDoublelist.Count() > 0)
        //        {
        //            selectDoublelist[0].InBoundQty += forGoodsStock.InBoundQty;
        //            selectDoublelist[0].OutBoundQty += forGoodsStock.OutBoundQty;
        //            selectDoublelist[0].RemainQty += forGoodsStock.RemainQty;
        //        }
        //        else if (forGoodsStock.IsUnpacked != "1")
        //        {
        //            list.Add(forGoodsStock);
        //        }
        //    }

        //    return list;
        //}

        public override List<GoodsStock> List(string parameters, ListFlags flags, int pageIndex, int pageSize, out int pageCount)
        {
            List<GoodsStock> targetStockList = new List<GoodsStock>(); //最终库存数据
            List<GoodsStock> childStockList = new List<GoodsStock>();//所有不重复SKU的数据

            if (!string.IsNullOrEmpty(parameters))
                return base.List(parameters,flags,pageIndex,pageSize,out pageCount);

            List<GoodsStock> allStockList = base.List("sgpin='PHCAD' and sgarea='LAX' and sgstorecode not in ('FE','OR','NJ','DE') and remainQty >0 ", ListFlags.Master, 0, 0, out pageCount);
            var sortedStockList = allStockList.OrderBy(x => x.SgCode).ToList(); //根据单号排序

            GoodsStock prevStockItem = new GoodsStock();//上一次sku数据
            GoodsStock targetStockItem = null;//每一个SKU/转运单号的数据
            foreach (var stock in sortedStockList) //循环数据库存表数据
            {
                //当前sku不等于上一个sku时添加新一条数据
                if (stock.SgCode != prevStockItem.SgCode)
                {
                    targetStockItem = new GoodsStock(); //新建一条库存数据
                    targetStockItem.StockDetails = new List<GoodsStock>();//新建一条库存明细数据

                    if (childStockList.Count == pageSize) //每页显示数量
                        break;

                    childStockList.Add(targetStockItem);//将每条不重复的单号添入数组
                }
                prevStockItem = stock;
                targetStockItem.StockDetails.Add(stock); //添加每个单号对应的明细

            }

            foreach (var clildstock in childStockList)//循环所有不重复SKU的数据
            {
                List<GoodsStock> goodsStockList = clildstock.StockDetails;//获取不重复sku明细数据
                if (goodsStockList.Count() == 1) //只有一条明细则添加最终库存数据数组 不添加明细数据
                {
                    targetStockList.Add(goodsStockList[0]);
                }
                if (goodsStockList.Count() > 1)
                {
                    //有多条明细则将明细数据入库数量，出库数量，可用数量累加组成该SKU明细总数据，并添加明细数据
                    int inBoundQty = 0;
                    int outBoundQty = 0;
                    int remainQty = 0;
                    targetStockItem = new GoodsStock();
                    foreach (var forStock in goodsStockList)//累加数量
                    {
                        inBoundQty += forStock.InBoundQty;
                        outBoundQty += forStock.OutBoundQty;
                        remainQty += forStock.RemainQty;
                    }
                    targetStockItem.StockDetails = goodsStockList; //添加明细
                    //总明细 重新赋值
                    targetStockItem.SgArea = goodsStockList[0].SgArea;
                    targetStockItem.SgCode = goodsStockList[0].SgCode;
                    targetStockItem.GoodsName = goodsStockList[0].GoodsName;
                    targetStockItem.SgLbs = goodsStockList[0].SgLbs;
                    targetStockItem.SgWunit = goodsStockList[0].SgWunit;
                    targetStockItem.SgPin = goodsStockList[0].SgPin;
                    targetStockItem.LogisticsCenterArea = goodsStockList[0].LogisticsCenterArea;
                    targetStockItem.InBoundQty = inBoundQty;
                    targetStockItem.OutBoundQty = outBoundQty;
                    targetStockItem.RemainQty = remainQty;
                    //将组合好的sku及sku明细添加到最终库存数据中
                    targetStockList.Add(targetStockItem);
                }
            }
            return targetStockList;

        }
       
    }
}
